#include <sstream>
#include <iostream>
#include "stdio.h"
#include <sys/socket.h>

#include "ConnectSocket.hpp"
#include "GenericException.hpp"

int protected_main(int argc, char **argv) {
	if (argc != 3) {
		std::cerr << "Syntax:" << std::endl;
		std::cerr << argv[0] << " <Host> <File>" << std::endl;
		exit(1);
	}

	ConnectSocket* socket = new ConnectSocket(argv[1], 4433);
	TCPConnection connection = socket->connect();

	FILE *f;
	f = fopen(argv[2], "r");

	char buffer[1024];
	int length = 0;
	do {
		length = fread(&buffer, sizeof(char), 1024, f);
		send(connection.getFD(), &buffer, sizeof(char) * length, 0);
	} while (length != 0);

	fclose(f);

	connection.close();

	return 0;
}

int main(int argc, char **argv) {
//	return protected_main(argc, argv);
	
	try {
		return protected_main(argc, argv);
	} catch (const GenericException &E) {
		E.terminate();
	}
	return 1;
}
